package com.hxzy.servlet.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author nick
 */
@WebServlet("/session1")
public class MySession1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session的对象；
        HttpSession session = req.getSession();
        //设置session最大有效时间
        //session.setMaxInactiveInterval(0); //0代表session永不失效
        session.setMaxInactiveInterval(30); //不设置，默认30分钟； session有效时间30秒

        System.out.println("session1:      " + session);
        //向session作用域中添加数据
        session.setAttribute("userName","admin");

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write("session1响应了一个页面");
        writer.close();
    }
}
